package com.example.myapplication;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2020/9/2
 * @description
 */
public class baiduWord {
    static volatile int index = 1;
    static ExecutorService pool;
    static volatile StringBuffer stringList = new StringBuffer();

    public static void main(String[] args) {
        pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            nextClass();
        }
    }

    public static void nextClass() {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                if (index < 218) {
                    String url = "https://fanyi.baidu.com/?aldtype=16047#en/zh/circle";
                    index++;
                    new Query2().getDns(url);
                }
            }
        });
        if (index == 217) {
            saveFile();
        }
    }

    public static void saveFile() {
        File file = new File("D:\\wrf\\qn\\"+"book"+".txt");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
//            String json = GsonUtil.toJson(stringList);
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(stringList.toString());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static class Query2 {
        public void getDns(String url) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                    if (response.body() != null) {
                        String html = response.body().string();
//                        System.out.println(html);
                        nextSub(html);
                        nextClass();
                    }
                }
            });
        }

        public void nextSub(String html) {
            String tag = "contents\">";
            int index = html.indexOf(tag);
            if (index > 0) {
                String subHtml1 = html.substring(index + tag.length() + 3);
                int indexP = subHtml1.indexOf("<p>");
                String subHtml2 = subHtml1.substring(indexP + 3);
                int last = subHtml2.indexOf("</div>");
                if (last > 0) {
                    String result = subHtml2.substring(0, last);
                    result = result.replaceAll("<p>", "").replaceAll("</p>", "");
                    System.out.println(result);
                    stringList.append(result+"\r\n");
                    if (index > 3) {
                        String nextHtml = subHtml2.substring(last);
                        nextSub(nextHtml);
                    }
                }
            }
        }
    }


}
